import axios from "axios";
import { ElMessage } from 'element-plus'
import useUserStore from '@/store/user'
let request = axios.create({
  baseURL: 'https://admin-api.macrozheng.com',
  timeout: 5000
})


request.interceptors.request.use(config => {
  let userStore = useUserStore();
  if (userStore.token) {
    // config.headers.token = userStore.token;
    config.headers['Authorization'] = userStore.token
  }
  // config.headers['Authorization'] = getToken()
  //返回配置对象
  return config;
})

request.interceptors.response.use((response) => {
  return response.data
}, (err) => {
  let message = '';
  let status = err.response.status;
  switch (status) {
    case 401:
      message = 'TOKEN过期';
      break;
    case 403:
      message = '无权访问';
      break;
    case 404:
      message = '请求地址错误';
      break;
    case 500:
      message = '服务器出现问题 ';
      break;
    default:
      message = '网络出现问题';
      break;
  }

  ElMessage({
    type: 'error',
    message,
  });
  return Promise.reject(err)
})


export default request

